package com.systop.payment.service.impl;

import com.systop.payment.entity.OrderSubjoinEntity;
import com.systop.payment.entity.PaytypeEntity;
import com.systop.payment.service.OrderSubjoinService;
import com.systop.payment.service.PaytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systop.common.utils.PageUtils;
import com.systop.common.utils.Query;

import com.systop.payment.dao.OrderDao;
import com.systop.payment.entity.OrderEntity;
import com.systop.payment.service.OrderService;
import org.springframework.transaction.annotation.Transactional;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    private OrderSubjoinService orderSubjoinService;

    @Autowired
    private PaytypeService paytypeService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params,"id",false),
                new QueryWrapper<OrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils findMainData(PageUtils page, List<OrderSubjoinEntity> subList) {
        //循环主表
        for (Object entity : page.getList()) {
            List addList = new ArrayList();

            //数据格式转换
            OrderEntity orderEntity = (OrderEntity)entity;
            // System.out.println("当前数据转换后:"+orderEntity);

            //循环从表
            for (OrderSubjoinEntity subEntity : subList) {
                //当主表的id与从表的父id相等时,向实体中的list添加当前数据
                if(orderEntity.getId()==subEntity.getParentId()){
                    addList.add(subEntity);
                }
            }
            orderEntity.setSubjoinList(addList);
        }

        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrder(OrderEntity order) throws Exception {
        //判断当前订单表中是否存在相同数据(客户,付款状态,商家)
        OrderEntity orderEntity = this.getOne(new QueryWrapper<OrderEntity>()
                .eq("customer", order.getCustomer())
                .eq("paytype", order.getPaytype())
                .eq("business", order.getBusiness())
        );
        System.out.println("是否存在相同数据:"+orderEntity==null);
        //如果不存在相同数据,就在订单主表中添加当前订单数据,存在则只更新备注
        if(orderEntity==null){
            System.out.println("不存在相同数据");
            //当数据不存在时需要判断该种需要添加的支付方式是否存在
            PaytypeEntity one = paytypeService.getOne(new QueryWrapper<PaytypeEntity>().eq("pay_name", order.getPaytype()));
            System.out.println("需要添加的支付方式是否存在:"+one);
            // System.out.println(one!=null);
            if(one!=null){
                //获取当前创建订单时间
                order.setCreatetime(new Date());
                //在主表中添加数据
                this.save(order);
                //获得主表相应数据
                orderEntity = this.getOne(new QueryWrapper<OrderEntity>()
                        .eq("customer", order.getCustomer())
                        .eq("paytype", order.getPaytype())
                        .eq("business", order.getBusiness())
                );
            }
            System.out.println(orderEntity);
        }else{
            System.out.println("存在相同数据");
            //如果备注不为空就更改备注
            if(order.getRemarks()!=null&&order.getRemarks()!=""){
                System.out.println("备注不为空");
                OrderEntity newEntity = new OrderEntity();
                //将备注更改为最新订单的备注
                newEntity.setId(orderEntity.getId());
                newEntity.setRemarks(orderEntity.getRemarks());
                System.out.println(newEntity);
                this.updateById(newEntity);
            }
        }

        //主表数据存在或添加成功才能添加子表
        if(orderEntity!=null){
            boolean b = orderSubjoinService.saveByParentId(orderEntity.getId(), order.getSubjoinList());
            if(!b){
                System.out.println("开始回滚事务");
                throw new Exception("出现异常");
            }
        }
    }
}